package cn.kgc.util;


import cn.kgc.util.resultutil.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author YC
 * 在过滤器中直接给前端返回封装的Result对象
 */
public class ResponseUtil {

    public static void setResponse(HttpServletResponse response, Result<Object> result) {
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 将Result对象转为json格式的数据 放到response的响应体中 返回给前端
            objectMapper.writeValue(response.getWriter(), result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
